package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DstoreExcution;
import com.fortune.dto.in.dStore.DstoreAddDto;
import com.fortune.dto.in.dStore.DstoreEditDto;
import com.fortune.dto.in.dStore.DstoreListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DstoreEnum;
import com.fortune.service.DstoreService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.dStore.DstoreInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/5/26 0026.
 */

@Api(value = "store", description = "经销商仓库设置", tags = {"经销商仓库设置"})
@RestController
@RequestMapping("store")
public class DstoreController {
    @Autowired
    private DstoreService dstoreService;

    @ApiOperation(value = "经销商仓库设置列表", nickname = "dStoreList", notes = "", tags={ "@安童" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="String"),
            @ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
            @ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<DstoreInfo>> list(HttpServletRequest request,@ApiIgnore @Validated DstoreListDto dstoreListDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        dstoreListDto.setPage((dstoreListDto.getPage()-1)*10);
        List<DstoreInfo> list = dstoreService.findStoreList(dstoreListDto,did);
        int count = dstoreService.findStoreListCount(dstoreListDto,did);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DstoreExcution<PagingData<DstoreInfo>>(pagingData, DstoreEnum.FIND_SUCCESS);
    }

    @ApiOperation(value = "添加仓库", nickname = "dStoreAdd", notes = "", tags={ "@安童" })
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DstoreAddDto dstoreAddDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        String id = UUIDUtil.get32UUID();
        int add = dstoreService.insertStore(dstoreAddDto,did,id);
        if(add > 0){
            return new DstoreExcution<Void>(null,DstoreEnum.ADD_SUCCESS);
        }else{
            if(add == -1){
                return new DstoreExcution<Void>(null,DstoreEnum.FIND_REPEAT_FALSE);
            }
            return new DstoreExcution<Void>(null,DstoreEnum.ADD_FALSE);
        }
    }

    @ApiOperation(value = "编辑仓库", nickname = "dStoreEdit", notes = "", tags={ "@安童" })
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DstoreEditDto editDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int add = dstoreService.editStore(editDto,did);
        if(add > 0){
            return new DstoreExcution<Void>(null,DstoreEnum.EDIT_SUCCESS);
        }else{
            if(add == -1){
                return new DstoreExcution<Void>(null,DstoreEnum.FIND_REPEAT_FALSE);
            }
            return new DstoreExcution<Void>(null,DstoreEnum.EDIT_FALSE);
        }
    }

    @ApiOperation(value = "删除仓库", nickname = "dStoreDelete", notes = "", tags={ "@安童" })
    @ApiImplicitParam(name="storeId", value="仓库id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="仓库id不能为空")String storeId) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int delete = dstoreService.deleteStore(storeId,did);
        if(delete > 0){
            return new DstoreExcution<Void>(null,DstoreEnum.DELETE_SUCCESS);
        }else{
            return new DstoreExcution<Void>(null,DstoreEnum.DELETE_FALSE);
        }
    }

}
